load("//test_utils.bzl", "assert_output")

# buildifier: disable=no-effect
ocaml_library(
    name = "ppx-record-selectors",
    srcs = ["record_selectors.ml"],
    deps = [
        "//third-party/ocaml:ppxlib",
    ],
) if not host_info().os.is_windows else None

# buildifier: disable=no-effect
ocaml_binary(
    name = "ppx",
    srcs = ["ppx_driver.ml"],
    compiler_flags = [
        "-linkall",
    ],
    deps = [
        ":ppx-record-selectors",
    ],
) if not host_info().os.is_windows else None

# buildifier: disable=no-effect
ocaml_binary(
    name = "ppx-record-selectors-test",
    srcs = ["ppx_record_selectors_test.ml"],
    compiler_flags = [
        "-ppx",
        "$(exe_target :ppx) --as-ppx",
    ],
) if not host_info().os.is_windows else None

# [Note: Use the 'expand' sub-target to see the effects of
#  preprocessor expansion]
# ========================================================
#
# (1) Generate elaborated source files (suffix: .pp.ml)
#
# ```
# loc=$("$BUCK2" build //ocaml/ppx:ppx-record-selectors-test --show-output | awk '{ print $2; }')
# $BUCK2 build //ocaml/ppx:'ppx-record-selectors-test[expand]'
# ```

# (2) Print the elaborated version of 'ppx_record_selectors_test.ml'.
#
# ```
# cat $(dirname "$loc")/_expandobj_/ppx_record_selectors_test.pp.ml
# ```

# buildifier: disable=no-effect
assert_output(
    name = "ppx-record-selectors-test-check",
    command = "$(exe_target :ppx-record-selectors-test)",
    output = "4 quux",
) if not host_info().os.is_windows else None
